package phone.example.com.a36kr.activity;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.adapter.newslistviewadapter.NewsAdapter;
import phone.example.com.a36kr.bean.NewsBean;
import phone.example.com.a36kr.nettool.NetTool;
import phone.example.com.a36kr.tools.BaseActivity;
import phone.example.com.a36kr.tools.MyApp;

/**
 * Created by dllo on 16/8/24.
 */
public class SearchMoreActivity extends BaseActivity {

    private String urlFront = "https://rong.36kr.com/api/mobi/news/search?keyword=";
    private String urlRear = "&page=1&pageSize=20";
    private ListView lv;
    private NewsAdapter adapter;
    private String url;
    private String sContent;

    private View viewHead;
    private String urlS;


    @Override
    protected int setLayout() {
        return R.layout.activity_search_more;
    }

    @Override
    protected void initView() {
        lv = bindView(R.id.search_more_lv);
        viewHead = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.search_head_essay_number, null);
        adapter = new NewsAdapter(MyApp.getContext());


    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        sContent = intent.getStringExtra("Keywords");
        initNet();

    }

    private void initNet() {

        try {
            urlS = URLEncoder.encode(sContent, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        url = urlFront + urlS + urlRear;
        NetTool mNetTool = new NetTool();
        mNetTool.getNetData(url, NewsBean.class, new NetTool.NetListener<NewsBean>() {

            @Override
            public void onSuccess(final NewsBean newsBean) {
                Log.d("SearchActivity11111", "newsBean:" + newsBean);

                Toast.makeText(SearchMoreActivity.this, "newsBean.getData().getTotalCount():" + newsBean.getData().getTotalCount(), Toast.LENGTH_SHORT).show();
                adapter.setNewsBean(newsBean);

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {

                        } else {
                            Intent intent = new Intent(SearchMoreActivity.this, NewsSecondActivity.class);
                            String mFeedId = new String(newsBean.getData().getData().get(position - lv.getHeaderViewsCount()).getFeedId());
                            Log.d("NewsFragment", mFeedId);
                            intent.putExtra("newsFeedId", mFeedId);
                            startActivity(intent);
                        }
                    }
                });
                lv.addHeaderView(viewHead);
                try {
                    TextView essayNum = (TextView) findViewById(R.id.search_essay_Num);
                    essayNum.setText("共搜索到" + newsBean.getData().getTotalCount() + "篇文章");
                } catch (Exception e) {
                    Toast.makeText(SearchMoreActivity.this, "有问题", Toast.LENGTH_SHORT).show();
                }

                lv.setAdapter(adapter);


            }

            @Override
            public void onError(String errorMsg) {

            }
        });

    }


}
